package controllers;

import java.util.Collection;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import services.ActorService;
import services.CommentService;
import services.PaintingService;
import domain.Comment;
import domain.Customer;

@Controller
@RequestMapping("/comment")
public class CommentController extends AbstractController {

	// Services
	// -----------------------------------------------------------------

	@Autowired
	CommentService commentService;
	
	@Autowired
	PaintingService paintingService;

	@Autowired
	ActorService actorService;

	// Constructors -----------------------------------------------------------

	public CommentController() {
		super();
	}

	// Listing ---------------------------------------------------------------

	@RequestMapping(value="/list", method = RequestMethod.GET )
	public ModelAndView list(@RequestParam int paintingId) {
		ModelAndView result;
		Collection<Comment> comments;
		comments = commentService.findCommentParentByPaintingId(paintingId);
		Comment comment = null;
		if(actorService.findByPrincipal() instanceof Customer){
			comment = commentService.create(paintingService.findOne(paintingId));
		}
		else{
			comment = new Comment();
		}
		result = new ModelAndView("comment/list");
		result.addObject("comments", comments);
		result.addObject("comment",comment);
		result.addObject("URI", "/comment/list.do");
		
		return result;
	}

	// Creation ---------------------------------------------------------------

	// Ancillary methods ------------------------------------------------------

}
